
class ListNode {
    public int val;
    public ListNode next;
    public ListNode head;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public void createList(int data1, int data2, int data3) {
        ListNode listNode1 = new ListNode(data1);
        ListNode listNode2 = new ListNode(data2);
        ListNode listNode3 = new ListNode(data3);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        head = listNode1;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
}

public class Test {
    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        } else if (list2 == null) {
            return list1;
        } else if (list1.val <= list2.val) {
            list1.next = mergeTwoLists(list1.next, list2);
            return list1;
        } else {
            list2.next = mergeTwoLists(list1, list2.next);
            return list2;
        }
    }
    public static void main(String[] args) {
        ListNode listNode1 = new ListNode();
        listNode1.createList(1, 3, 4);
        ListNode listNode2 = new ListNode();
        listNode2.createList(1, 2, 4);
        mergeTwoLists(listNode1.head,listNode2.head);
        ListNode cur = listNode1.head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
}
